package model.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.dto.Card;
import utility.Database;
import utility.Log;

public class CardModel extends Database {
	public boolean insert(Card dto) {
		String sql = "insert into tbl_card(card_id,card_minute,card_type,player_id,match_id) values(null,?,?,?,?)";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, dto.getMinute());
			pstmt.setInt(2, dto.getType());
			pstmt.setInt(3, dto.getPlayerId());
			pstmt.setInt(4, dto.getMatchId());
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}

		return false;
	}

	public boolean delete(int card_id) {
		String sql = "delete from tbl_card where card_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, card_id);
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return false;
	}

	public boolean update(Card dto) {
		String sql = "update tbl_card set card_minute=?,card_type=?,player_id=? where card_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, dto.getMinute());
			pstmt.setInt(2, dto.getType());
			pstmt.setInt(3, dto.getPlayerId());
			pstmt.setInt(4, dto.getId());
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return false;
	}

	public Card get(int card_id) {
		String sql = "select * from tbl_card where card_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, card_id);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				Card dto = new Card();
				dto.setId(card_id);
				dto.setMinute(rs.getInt("card_minute"));
				dto.setPlayerId(rs.getInt("player_id"));
				dto.setType(rs.getInt("card_type"));
				dto.setMatchId(rs.getInt("match_id"));
				return dto;
			}
		} catch (SQLException e) { 
			Log.write(e);
		} finally {
			this.close();
		}
		return null;
	}

	public ResultSet list(String search, int limit, int page) {
		String sql = "select * from tbl_country where country_name like ? ORDER BY country_id DESC limit ?,?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "%" + search + "%");
			pstmt.setInt(2, (page - 1) * limit);
			pstmt.setInt(3, limit);
			ResultSet rs = pstmt.executeQuery();
			return rs;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return null;
	}

	public ResultSet list(int limit, int page) {
		return list("", limit, page);
	}

	public int count(String search) {
		String sql = "select count(*) from tbl_country where country_name like ?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "%" + search + "%");
			ResultSet rs = pstmt.executeQuery();
			return rs.next() ? rs.getInt(1) : 0;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return 0;
	}

	public int count() {
		return count("");
	}

}
